package y_2025.m03.d17.t01;

import java.util.Scanner;

public class QuadraticEquation {
    // 成员变量
    private double a;
    private double b;
    private double c;

    // 构造函数
    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // 计算方程的解
    public void computeRoot() {
        if (a == 0) {
            System.out.println("系数a不能为0，这不是一个二次方程。");
            return;
        }

        double discriminant = b * b - 4 * a * c;

        if (discriminant > 0) {
            double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.println("方程有两个不同的实根: " + root1 + " 和 " + root2);
        } else if (discriminant == 0) {
            double root = -b / (2 * a);
            System.out.println("方程有一个实根: " + root);
        } else {
            System.out.println("方程没有实根，有两个复数根。");
        }
    }

    // 测试程序
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("系数a：");
        int a = scanner.nextInt();

        System.out.println("系数b：");
        int b = scanner.nextInt();

        System.out.println("系数c：");
        int c = scanner.nextInt();
        QuadraticEquation equation = new QuadraticEquation(a, b, c);
        equation.computeRoot();
    }
}